﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using OTA.Notification.BO.Dictionary;
using OTA.Notification.Entity.Dictionary;
using Strive.DataAccess.Base.Model.SearcherObject;
using Strive.DataAccess.Base.Model.PageObject;
using System.Xml;
using System.Collections.Generic;


namespace OTA.Notification.Web.Dictionary
{ 
    public partial class diclist : System.Web.UI.Page
    {

        private string _strLan = "en-us";

        private const string SESSION_PAGE_COUNT = "SESSION_PAGE_COUNT";

        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                InitPage();

                if (Request.QueryString["action"] != null)
                {
                    //get data
                    if (Request.QueryString["action"].ToLower() == "get")
                    {
                        int iPageIndex = 1;
                        int iPageSize = 15;

                        string strCode = string.Empty;
                        string strLan = string.Empty;

                        string strQuery = string.Empty;

                        if (!string.IsNullOrEmpty(Request.Params["page"]))
                            int.TryParse(Request.Params["page"], out iPageIndex);

                        if (!string.IsNullOrEmpty(Request.Params["rp"]))
                            int.TryParse(Request.Params["rp"], out iPageSize);

                        if (!string.IsNullOrEmpty(Request.Params["query"]))
                            ParseQuery(Request.Params["query"], out strCode, out strLan);

                        Response.Write(GetJSON(iPageIndex, strCode, strLan, iPageSize));
                        Response.End();

                    }

                }

            }
        }

        private void InitPage()
        {

        }

        /// <summary>
        /// 解析查询
        /// </summary>
        /// <param name="Query"></param>
        /// <param name="Room"></param>
        /// <param name="State"></param>
        private void ParseQuery(string Query, out string Code, out string Lan)
        {

            string[] sp = Query.Split(',');

            Code = string.Empty;
            Lan = _strLan;

            if (sp.Length == 2)
            {
                Code = sp[0];
                Lan = sp[1];
            }
        }


        private string GetJSON(int PageIndex, string Code, string Lan, int PageSize)
        {

            string strJson = "";

            using (DictionaryBO bo = new DictionaryBO())
            {

                BasePageSearch bs = new BasePageSearch();

                bs.PageSize = PageSize;
                bs.PageIndex = PageIndex;

                bs.Column = "ID,dbo.f_GetContentName([NAME],'" + Lan + "') AS [Name],code,create_time,parent_code,value";

                if (!string.IsNullOrEmpty(Code))
                {
                    bs.Where += " AND parent_code='" + Code + "' ";
                }
                else
                {
                    bs.Where += " AND parent_code='' ";
                }

                bs.Order = " ORDER BY ID DESC";

                PageResult<OTA_DICTIONARY> pr = bo.getDictionaryPageList(bs);

                //get page count from viewstate

                int iViewStatePageCount = 0;

                if (Session[SESSION_PAGE_COUNT] != null)
                    int.TryParse(Session[SESSION_PAGE_COUNT].ToString(), out iViewStatePageCount);

                if (pr.RecordCount == 0)
                    pr.RecordCount = iViewStatePageCount;
                else
                    Session[SESSION_PAGE_COUNT] = pr.RecordCount;

                strJson = "{\"total\":" + pr.RecordCount + ",\"page\":" + PageIndex.ToString() + ",\"rows\":[";

                foreach (OTA_DICTIONARY bean in pr.Data)
                {

                    strJson += "{\"id\":" + bean.ID.ToString() + ",";
                    strJson += "\"cell\":[";

                    strJson += "\"" + bean.ID.ToString() + "\",";
                    strJson += "\"" + HttpUtility.HtmlEncode(bean.PARENT_CODE) + "\",";
                    strJson += "\"" + HttpUtility.HtmlEncode(bean.CODE) + "\",";

                    if (bean.CODE != "" && bean.CODE != null)
                    {
                        strJson += "\"<a href='#'>" + HttpUtility.HtmlEncode(bean.NAME) + "</a>\",";
                    }
                    else
                    {
                        strJson += "\"" + HttpUtility.HtmlEncode(bean.NAME) + "\",";
                    }

                    strJson += "\"" + HttpUtility.HtmlEncode(bean.VALUE.ToString()) + "\",";
                    strJson += "\"" + bean.CREATE_TIME.ToString() + "\"";

                    strJson += "]},";
                }


            }

            //...

            strJson = strJson.Replace("\r\n", " ");

            strJson = strJson.TrimEnd(',');
            strJson += "]}";

            return strJson;
        }
    }
}
